package com.example.drink_order_system.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.AppCompatButton;

import com.example.drink_order_system.R;
import com.example.drink_order_system.entity.task;

import java.util.ArrayList;

import DataBase.UserDbHelper;

public class JiFen_detail extends AppCompatActivity {
    private ImageButton back;
    private AppCompatButton shop;
    private ListView task_list;
    private TextView my_score;
    private my_adapter myAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_ji_fen_detail);
        back = findViewById(R.id.my_score_page_back);
        shop = findViewById(R.id.score_exchange_shop);
        task_list = findViewById(R.id.task_list);
        my_score = findViewById(R.id.score);


        my_score.setText(String.valueOf(MainActivity.now_user.getScore())+"\n古达积分");
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        myAdapter = new my_adapter(task.getTasks());
        task_list.setAdapter(myAdapter);
        shop.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(JiFen_detail.this,shop_page.class);
                startActivity(intent);
                if (!task.getTasks().get(5).isIs_done()){
                    task.getTasks().get(5).setIs_done(true);
                    //刷新积分并保存
                    MainActivity.now_user.setScore(MainActivity.now_user.getScore()+task.getTasks().get(5).getScore());
                    UserDbHelper db = (UserDbHelper) UserDbHelper.getSqliteHelper(getApplicationContext());
                    db.saveScore(MainActivity.now_user.getUsername(),MainActivity.now_user.getScore());
                }
            }
        });
    }

     class my_adapter extends BaseAdapter{
        private ArrayList<task> tasks;
        private AppCompatButton complete_task;
        private TextView task_detail;
        private TextView add_score;
        @Override
        public boolean hasStableIds() {
            return super.hasStableIds();
        }

        @Override
        public Object getItem(int i) {
            return null;
        }

        @Override
        public long getItemId(int i) {
            return 0;
        }

        public my_adapter(ArrayList<task> tasks) {
            super();
            this.tasks = tasks;
        }

        @Override
        public int getCount() {
            return tasks.size();
        }

        @Override
        public View getView(int i, View view, ViewGroup viewGroup) {
            view = View.inflate(getApplicationContext(),R.layout.singel_task_layout,null);
            task_detail = view.findViewById(R.id.task_detail);
            add_score = view.findViewById(R.id.add_score_text);
            complete_task = view.findViewById(R.id.complete_task);
            task_detail.setText(tasks.get(i).getTask_detail());
            add_score.setText("+" + tasks.get(i).getScore()+"古达积分");


            complete_task.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    switch (i){
                        case 0:
                            finish();
                            break;
                        case 1:
                            finish();
                            RootActivity.getIns().goTo_home();
                            Toast.makeText(JiFen_detail.this, "请前往点单页面进行点单！", Toast.LENGTH_SHORT).show();
                            break;
                        case 2:
                            finish();
                            Toast.makeText(JiFen_detail.this, "请将该应用分享给好友！", Toast.LENGTH_SHORT).show();
                            break;
                        case 3:
                            finish();
                            break;
                        case 4:
                            finish();
                            Toast.makeText(JiFen_detail.this, "请前往完善你的基本信息！", Toast.LENGTH_SHORT).show();
                            break;
                        case 5:
                            Intent intent = new Intent(JiFen_detail.this,shop_page.class);
                            startActivity(intent);
                            if (!tasks.get(5).isIs_done()){
                                task.getTasks().get(5).setIs_done(true);
                                //刷新积分并保存
                                MainActivity.now_user.setScore(MainActivity.now_user.getScore()+tasks.get(5).getScore());
                                UserDbHelper db = (UserDbHelper) UserDbHelper.getSqliteHelper(getApplicationContext());
                                db.saveScore(MainActivity.now_user.getUsername(),MainActivity.now_user.getScore());
                            }
                            break;
                        case 6:
                            finish();
                            Intent intent1 = new Intent(JiFen_detail.this,discount_detail_page.class);
                            startActivity(intent1);
                            Toast.makeText(JiFen_detail.this, "请前往使用优惠券！", Toast.LENGTH_SHORT).show();
                            break;
                    }
                }
            });

            if (tasks.get(i).isIs_done()){
                complete_task.setText("已完成");
                complete_task.setBackgroundResource(R.drawable.task_is_done);
                complete_task.setEnabled(false);
            }
            return view;
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        myAdapter.notifyDataSetChanged();
        my_score.setText(String.valueOf(MainActivity.now_user.getScore()));
    }
}